# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=soci
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.0.3
pkgrel=5
pkgdesc="SOCI - The C++ Database Access Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://soci.sourceforge.io/"
msys2_repository_url="https://github.com/SOCI/soci"
msys2_references=(
  'aur: soci'
)
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-firebird")
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-sqlite3")
optdepends=("${MINGW_PACKAGE_PREFIX}-libmariadbclient: support for MySQL databases"
            "${MINGW_PACKAGE_PREFIX}-postgresql: support for PostGreSQL databases"
            "${MINGW_PACKAGE_PREFIX}-sqlite3: support for SQLite databases")
if [[ ${CARCH} != aarch64 ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-firebird: support for Firebird databases")
fi
source=("https://github.com/SOCI/soci/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        soci-4.0.0.patch
        soci-cmake-sociconfig.patch
        soci-tests-catch.patch)
sha256sums=('4b1ff9c8545c5d802fbe06ee6cd2886630e5c03bf740e269bb625b45cf934928'
            'fe7db55531f0d061cced835709065672904bb00d26514e0c323124bab43f2b90'
            '65f820a25a8c19e1bd608372f7092db88b2428b529cb05622574527a6ca4591c'
            '2490363693b9350527f6e0115123d99fb375f701f7b241992cccb18c59940cb7')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/soci-4.0.0.patch
  patch -p1 -i "${srcdir}"/soci-cmake-sociconfig.patch
  patch -p1 -i "${srcdir}"/soci-tests-catch.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/SOCI/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE_1_0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
